﻿/*
Commerce by Onex – eCommerce software platform (www.ecommerce.byonex.com)
Copyright (C) 2008-2010 by Onex Group

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 */
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ShopBLL;
using ShopModel;

public partial class cms_Properties : System.Web.UI.Page
{

    #region CONSTANTS

    private const string DELETE_ATTEMPT = "DeleteAttempt";

    #endregion CONSTANTS

    #region VARIABLES

    Category CurrentCategory = null;

    #endregion VARIABLES

    #region PROPERTIES

    protected int SelectedCategoryID
    {
        get
        {
            if (tvCategory.SelectedNode != null)
                return int.Parse(tvCategory.SelectedNode.Value);
            else
                return -1;
        }
        set
        {
            tvCategory.SelectNode(value, false);
        }
    }

    bool DeleteAttempt
    {
        get
        {
            if (ViewState[DELETE_ATTEMPT] != null)
                return (bool)ViewState[DELETE_ATTEMPT];
            else
                return false;
        }
        set
        {
            ViewState[DELETE_ATTEMPT] = value;
        }
    }

    #endregion PROPERTIES

    #region METHODS

    protected void RefreshMenu()
    {
        CategoryList newCategoryList = new CategoryList();

        newCategoryList.LoadCategory(ProductsManager.Instance.GetCategories(true, ShopBLL.CommonTools.CurrentLanguageID));

        //if (Application["CategoryList"] == null)
        //    Application.Add("CategoryList", newCategoryList);
        //else
        //    Application["CategoryList"] = newCategoryList;
    }

    protected void DisableSubcategories(int CategoryID, DropDownList list)
    {
        string categoryPath = list.Items.FindByValue(CategoryID.ToString()).Text; ;

        foreach (ListItem tmpListItem in list.Items)
        {
            if (tmpListItem.Text.StartsWith(categoryPath))
                tmpListItem.Enabled = false;
            else
                tmpListItem.Enabled = true;
        }
    }

    protected void RefreshCategoryData()
    {
        if (SelectedCategoryID > 0)
        {
            CurrentCategory = ProductsManager.Instance.GetCategoryByID(SelectedCategoryID, int.Parse(ddlLanguage.SelectedValue));

            if (CurrentCategory == null)
            {
                CtrlShowInfo.ShowInfo(Resources.Language.cmsMessageUnexpectedError, true);
            }
            else
            {
                tbDescription.Text = CurrentCategory.Description;
                tbCategoryName.Text = CurrentCategory.Name;
                txtWeight.Text = CurrentCategory.Importance.ToString();
                chbActive.Checked = CurrentCategory.IsActive;

                DisableSubcategories(CurrentCategory.IDcategory, ddlParent);

                ddlParent.SelectedValue = CurrentCategory.ParentID.ToString();
                ddlViews.SelectedValue = CurrentCategory.ViewId == null ? "-1" : CurrentCategory.ViewId.ToString();

                btnChangeTop.Enabled = true;
                btnChangeBottom.Enabled = true;
                btnRemoveTop.Enabled = true;
                btnRemoveBottom.Enabled = true;
            }
        }
        else
        {
            tbDescription.Text = string.Empty;
            tbCategoryName.Text = string.Empty;
            txtWeight.Text = string.Empty;
            chbActive.Checked = false;

            btnChangeTop.Enabled = false;
            btnChangeBottom.Enabled = false;
            btnRemoveTop.Enabled = false;
            btnRemoveBottom.Enabled = false;
        }

        DeleteAttempt = false;

    }

    protected void ClearData()
    {
        DeleteAttempt = false;

        tbDescription.Text = string.Empty;
        tbCategoryName.Text = string.Empty;
        txtWeight.Text = "0";
        chbActive.Checked = false;
    }

    protected void RefreshData()
    {
        Category categoryTree = ProductsManager.Instance.GetChildernTreeCategories(-1, false, ShopBLL.CommonTools.CurrentLanguageID);

        tvCategory.GenerateCategoryTree();

        string _selectedvalue = ddlParent.SelectedValue;

        ddlParent.Items.Clear();
        ddlParent.Items.Add(new ListItem("(" + Resources.Language.MainCategory + ")", "-1"));

        CommonTools.FillCategoryTree(categoryTree, ddlParent, true);

        ddlParent.SelectedValue = _selectedvalue;
    }

    protected void FillViewList()
    {
        List<ShopModel.View> views = ProductsManager.Instance.GetViews(CommonTools.CurrentLanguageID);
        ddlViews.DataSource = views;
        ddlViews.DataBind();
        ddlViews.Items.Insert(0, new ListItem(Resources.Language.DefaultView, "-1"));
    }

    #endregion METHODS

    #region EVENTS

    protected void btnChange_Click(object sender, EventArgs e)
    {
        int weight = 0;
        int.TryParse(txtWeight.Text, out weight);

        Category category = new Category();

        category.Name = tbCategoryName.Text;
        category.Description = tbDescription.Text;
        category.IDcategory = SelectedCategoryID;
        category.Importance = weight;
        category.ParentID = int.Parse(ddlParent.SelectedValue);
        category.IsActive = chbActive.Checked;
        category.ViewId = ddlViews.SelectedValue == "-1" ? null : (int?)Convert.ToInt32(ddlViews.SelectedValue);

        int NewCategoryID = ProductsManager.Instance.SaveCategory(category, int.Parse(ddlLanguage.SelectedValue));

        if (NewCategoryID > 0)
        {
            RefreshData();
            RefreshMenu();
            DisableSubcategories(NewCategoryID, ddlParent);
            SelectedCategoryID = NewCategoryID;
            CtrlShowInfo.ShowInfo(Resources.Language.cmsMessageDataSaveSuccessful, false);
        }
        else
        {
            CtrlShowInfo.ShowInfo(Resources.Language.cmsMessageDataSaveFailure, true);
        }
    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
        hfEditMode.Value = "true";
        ClearData();

        foreach (ListItem li in ddlParent.Items)
            li.Enabled = true;

        if (tvCategory.SelectedNode != null)
            tvCategory.SelectedNode.Selected = false;
    }

    protected void btnRemove_Click(object sender, EventArgs e)
    {
        if (SelectedCategoryID > 0)
        {
            if (!ProductsManager.Instance.CheckIfProductsExistInSubcategories(SelectedCategoryID))
            {
                if (!ProductsManager.Instance.DeleteCategory(SelectedCategoryID, DeleteAttempt))
                {
                    if (!DeleteAttempt)
                    {
                        DeleteAttempt = true;
                        CtrlShowInfo.ShowInfo(Resources.Language.cmsMessageCategoryHasNotDeletedSubcategories, true);
                    }
                    else
                        CtrlShowInfo.ShowInfo(Resources.Language.cmsCategoriesMessage8, true);
                }
                else
                {
                    int ParentID = int.Parse(tvCategory.SelectedNode.Parent.Value);
                    DeleteAttempt = false;
                    ClearData();
                    RefreshData();
                    tvCategory.SelectNode(ParentID, true);
                    CtrlShowInfo.ShowInfo(Resources.Language.CategoryDeleted, false);
                }
            }
            else
            {
                CtrlShowInfo.ShowInfo(Resources.Language.cmsCategoriesMessage7, true);
            }
        }
    }

    protected void tvCategory_SelectedNodeChanged(object sender, EventArgs e)
    {
        if (SelectedCategoryID > 0)
            hfEditMode.Value = "true";
        else
            hfEditMode.Value = "false";

        RefreshCategoryData();
    }

    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        RefreshCategoryData();
    }

    #endregion EVENTS

    #region PAGE EVENTS

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddlLanguage.DataSource = ReportsManager.Instance.GetAvailableLanguages(true);
            ddlLanguage.DataBind();

            ddlLanguage.SelectedValue = ShopBLL.CommonTools.CurrentLanguageID.ToString();

            FillViewList();

            RefreshData();
        }
    }

    protected void Page_LoadComplete(object sender, EventArgs e)
    {
        btnRemoveTop.Enabled = SelectedCategoryID > 0;
        btnRemoveBottom.Enabled = SelectedCategoryID > 0;
        btnRemoveTop.Text = DeleteAttempt ? Resources.Language.DeleteWithSubcategories : Resources.Language.DeleteUpper;
        btnRemoveBottom.Text = DeleteAttempt ? Resources.Language.DeleteWithSubcategories : Resources.Language.DeleteUpper;

        if (bool.Parse(hfEditMode.Value))
        {
            editPanel.Visible = true;
            btnChangeTop.Enabled = true;
            btnChangeBottom.Enabled = true;
        }
        else
        {
            editPanel.Visible = false;
            btnChangeTop.Enabled = false;
            btnChangeBottom.Enabled = false;
        }
    }

    #endregion PAGE EVENTS
}